package com.pocketsweet.ui.fragments;

import java.io.ByteArrayOutputStream;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Base64;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.pocketsweet.R;
import com.pocketsweet.service.UserService;
import com.pocketsweet.ui.uilib.MyBitmapFactory;
import com.pocketsweet.ui.uilib.myTextViewShow;

public class WelcomeThree extends Fragment {
	@ViewInject(R.id.mtvShow)
	private myTextViewShow mtvShow;

	@ViewInject(R.id.welcome3)
	private ImageView welcome3;

	private boolean isShow = false;
	private static int IMAGE_MAX_WIDTH = 48;
	private static int IMAGE_MAX_HEIGHT = 96;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_welcome_3, null);
		ViewUtils.inject(this, rootView);
		return rootView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
//		 welcome3.setImageDrawable(getResources().getDrawable(
//		 R.drawable.welcome3));
//		Picasso.with(getActivity()).load(R.drawable.welcome3).into(welcome3);  
		UserService.displaySplashImage(R.drawable.welcome3, welcome3);

//		welcome3.setImageBitmap(MyBitmapFactory
//				.decodeSampledBitmapFromResource(getResources(),
//						R.drawable.welcome3, 260, 596));

//		setImageSrc(welcome3,
//				drawableToByte(getResources().getDrawable(R.drawable.welcome3)));
	}

	public synchronized String drawableToByte(Drawable drawable) {

		if (drawable != null) {
			Bitmap bitmap = Bitmap
					.createBitmap(
							drawable.getIntrinsicWidth(),
							drawable.getIntrinsicHeight(),
							drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
									: Bitmap.Config.RGB_565);
			Canvas canvas = new Canvas(bitmap);
			drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
					drawable.getIntrinsicHeight());
			drawable.draw(canvas);
			int size = bitmap.getWidth() * bitmap.getHeight() * 4;

			// 创建一个字节数组输出流,流的大小为size
			ByteArrayOutputStream baos = new ByteArrayOutputStream(size);
			// 设置位图的压缩格式，质量为100%，并放入字节数组输出流中
			bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
			// 将字节数组输出流转化为字节数组byte[]
			byte[] imagedata = baos.toByteArray();

			String icon = Base64.encodeToString(imagedata, Base64.DEFAULT);
			return icon;
		}
		return null;
	}

	private void setImageSrc(ImageView imageView, String imagePath) {
		BitmapFactory.Options option = new BitmapFactory.Options();
		option.inSampleSize = getImageScale(imagePath);
		Bitmap bm = BitmapFactory.decodeFile(imagePath, option);
		imageView.setImageBitmap(bm);
	}

	/**
	 * scale image to fixed height and weight
	 * 
	 * @param imagePath
	 * @return
	 */
	private static int getImageScale(String imagePath) {
		BitmapFactory.Options option = new BitmapFactory.Options();
		// set inJustDecodeBounds to true, allowing the caller to query the
		// bitmap info without having to allocate the
		// memory for its pixels.
		option.inJustDecodeBounds = true;
		BitmapFactory.decodeFile(imagePath, option);

		int scale = 1;
		while (option.outWidth / scale >= IMAGE_MAX_WIDTH
				|| option.outHeight / scale >= IMAGE_MAX_HEIGHT) {
			scale *= 2;
		}
		return scale;
	}

	public void textShow() {
		if (isShow == false) {
			isShow = true;
			mtvShow.startShow();
		} else {
			mtvShow.setText(mtvShow.getTextString());
		}
	}


	@Override
	public void onDestroy() {
		super.onDestroy();
		releaseImageViews();

	}


	private void releaseImageViews() {
		MyBitmapFactory.releaseImageViewResouce(welcome3);
	}
}
